package com.szy.douyou_gateway.web;


import com.szy.douyou_article.dao.Article;
import com.szy.douyou_article.dao.State;
import com.szy.douyou_article.dao.req.ArticleReq;
import com.szy.douyou_gateway.client.ArticleClient;
import com.szy.douyou_gateway.config.AccountProfile;
import com.szy.douyou_gateway.config.ResultDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 帖子controller
 * @author sunziyan
 * @date 2022/4/21
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleClient articleClient;

    /**
     * 分页条件查询帖子
     *
     * @param articleReq
     * @return
     */
    @PostMapping("/queryPage")
    public ResultDTO queryPage(@RequestBody ArticleReq articleReq) {
        return articleClient.queryPage(articleReq);
    }


    /**
     * 查询一篇帖子
     *
     * @param articleId
     * @return
     */
    @GetMapping("/queryOne/{articleId}")
    public ResultDTO queryOne(@PathVariable("articleId") Long articleId) {
        return articleClient.queryOne(articleId);
    }

    @RequiresAuthentication
    @PostMapping("/editOrAdd")
    public ResultDTO editOrAdd(@Validated @RequestBody Article article) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (article.getArticleId() != null) {

            Article result = articleClient.queryOne(article.getArticleId()).getData();
            //如果是作者或系统管理员,允许编辑
            if (result.getUserId().equals(principal.getUserId()) || principal.getUserRight().equals(1)) {
                return articleClient.updateOne(article);
            }
            return ResultDTO.fail("没有编辑权限");
        } else {
            article.setUserId(principal.getUserId());
            return articleClient.addOne(article);
        }


    }


    /**
     * 删除一篇帖子及其评论
     *
     * @param articleId
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/deleteOne/{articleId}")
    public ResultDTO deleteOne(@PathVariable("articleId") Long articleId) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();

        Article article = articleClient.queryOne(articleId).getData();
        //如果是作者或系统管理员,允许删除
        if (article.getUserId().equals(principal.getUserId()) || principal.getUserRight().equals(1)) {
            return articleClient.deleteOne(articleId);
        }

        return ResultDTO.fail("没有删除权限");

    }

    /**
     * 帖子浏览量加一
     *
     * @param articleId
     * @return
     */
    @GetMapping("/addArticleViews/{articleId}")
    public ResultDTO addArticleViews(@PathVariable("articleId") Long articleId) {
        return articleClient.addArticleViews(articleId);
    }

    /**
     * 获取文章好评差评情况
     *
     * @param articleId
     * @return
     */
    @GetMapping("/getAllState/{articleId}")
    public ResultDTO getAllState(@PathVariable("articleId") Long articleId) {
        return articleClient.getAllState(articleId);
    }

    /**
     * 获取用户在帖子的评价情况
     *
     * @param articleId
     * @param userId
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/getMyState/{articleId}/{userId}")
    public ResultDTO getMyState(@PathVariable("articleId") Long articleId, @PathVariable("userId") Long userId) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(userId)) {
            return ResultDTO.fail("禁止查看他人评价");
        }
        return articleClient.getMyState(articleId, userId);
    }

    /**
     * 创建用户在帖子中新评价
     *
     * @param state
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/createState")
    public ResultDTO createState(@RequestBody State state) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(state.getUserId())) {
            return ResultDTO.fail("禁止创建他人评价");
        }

        return articleClient.createState(state);
    }

    /**
     * 修改用户在帖子中评价
     *
     * @param state
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/changeState")
    public ResultDTO changeState(@RequestBody State state) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();
        if (!principal.getUserId().equals(state.getUserId())) {
            return ResultDTO.fail("禁止修改他人评价");
        }
        return articleClient.changeState(state);
    }


}
